package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.rediskey.RedisKeys;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import javafx.fxml.LoadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static cn.wolfcode.wolf2w.util.JsonResult.CODE_ERROR_PARAM;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    private int a = 5;

    /**
     * 根据 id 获取单个用户
     * @param id
     * @return UserInfo
     */
    @GetMapping("/get")
    public UserInfo get(String id){
        return userInfoService.get(id);
    }

    /**
     * 根据 phone 查询手机号码
     * @param phone
     * @return
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    /**
     * 根据 phone 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoRedisService.saveCount(phone);
        userInfoService.sendVerifyCode(phone);
        userInfoRedisService.clickFrequency(phone);
        return JsonResult.success();
    }


    /**
     * 注册
     * @param nickname
     * @param rpassword
     * @param password
     * @param phone
     * @param verifyCode
     * @return 返回操作信息
     */
    @PostMapping("/regist")
    public Object regist(String nickname, String rpassword,String password, String phone, String verifyCode){
        userInfoService.regist(nickname,rpassword,password,phone,verifyCode);
        return JsonResult.success();
    }


    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Object login(String username, String password, HttpServletResponse response,HttpServletRequest request){
        UserInfo user = userInfoService.login(username,password);
        // 创建令牌
        String token = UUID.randomUUID().toString();
        // 将令牌设置到请求头
        response.setHeader("token",token);
        // 以用户令牌作为 key , user 对象 作为 value 存入 redis 中
        userInfoRedisService.saveVerifyCode(RedisKeys.TOKEN.getRedisKey(token), JSON.toJSONString(user));
        String tokenCode = userInfoRedisService.getVerifyCode(RedisKeys.TOKEN.getRedisKey(token));
        // 以 JsonResult 的格式返回到前端页面
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        // 设置 token 到请求头
        response.setHeader("token",token);
        return JsonResult.success(map);
    }


}
